package com.itheima.Jiuye05;

public class MyBinarySearchDemo {
    public static void main(String[] args) {
        int [] a = {1,2,3,4,5,6,7,8,9,10};
        int num = 9;
        int indext = BinarySearchForIndex(a, num);
        System.out.println(indext);
    }

    private static int BinarySearchForIndex(int[] a,int num) {
        int min = 0;
        int max = a.length-1;
        for (int i = 0; i < a.length-1; i++) {
            int mid = (max+min)>>1;
            if (a[mid] > num){
                max = mid -1;
            }else if (a[mid] < num){
                min = max+1;
            }else {
                return min;
            }
        }
        return -1;
    }


}
